package com.vote.controllers;

import java.util.List;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.ReqMapping;

import org.springframework.beans.factory.annotation.Autowired;

import com.vote.annotation.TopListRequired;
import com.vote.model.Questionnaire;
import com.vote.services.GroupService;
import com.vote.services.QuestionnaireService;

@TopListRequired
public class QListController{
		
		@Autowired
		private QuestionnaireService questionnaireService;
		
		@Autowired
		private GroupService groupService;
		
		@ReqMapping(path="newlist/{curpage:\\d+}")
		public String NewList(Invocation inv, @Param("curpage")int curpage){
			
			int itemPerPage = 10;
			int qCount = questionnaireService.getNewQuestionnaireListCount();
			
			List<Questionnaire> qList = questionnaireService.getNewQuestionnaireList(curpage * itemPerPage, itemPerPage); 
			
			inv.addModel("listHead", "最新投票");
			inv.addModel("qCount", qCount);
			inv.addModel("qList", qList);
			inv.addModel("curpage", curpage);
			inv.addModel("itemPerPage", itemPerPage);
			inv.addModel("nav", "new");
			return "qList";
		}
		
		@ReqMapping(path="hotlist/{curpage:\\d+}")
		public String HotList(Invocation inv, @Param("curpage")int curpage){
			
			int itemPerPage = 10;
			int qCount = questionnaireService.getHotQuestionnaireListCount();
			
			List<Questionnaire> qList = questionnaireService.getHotQuestionnaireList(curpage * itemPerPage, itemPerPage); 
			
			inv.addModel("listHead", "最热投票");
			inv.addModel("qCount", qCount);
			inv.addModel("qList", qList);
			inv.addModel("curpage", curpage);
			inv.addModel("itemPerPage", itemPerPage);
			inv.addModel("nav", "hot");
			return "qList";
		}
		
		@ReqMapping(path="typelist/{type:\\d+}/{curpage:\\d+}")
		public String TypeList(Invocation inv, @Param("curpage")int curpage, @Param("type")int type){
			
			int itemPerPage = 10;
			int qCount = questionnaireService.getQuestionnaireListCountByType(type);
			
			List<Questionnaire> qList = questionnaireService.getQuestionnaireListByType(type, curpage * itemPerPage, itemPerPage); 
			
			inv.addModel("listHead", "类型");
			inv.addModel("qCount", qCount);
			inv.addModel("qList", qList);
			inv.addModel("qType", type);
			inv.addModel("curpage", curpage);
			inv.addModel("itemPerPage", itemPerPage);
			inv.addModel("nav", "type");
			return "qList";
		}
		
		@ReqMapping(path="grouplist/{group:\\d+}/{curpage:\\d+}")
		public String GroupList(Invocation inv, @Param("curpage")int curpage, @Param("group")int group){
			
			int itemPerPage = 10;
			int qCount = questionnaireService.getQuestionnaireListCountByGroup(group);
			
			List<Questionnaire> qList = questionnaireService.getQuestionnaireListByGroup(group, curpage * itemPerPage, itemPerPage); 
			
			inv.addModel("listHead", "分类："+groupService.getGroupNameById(group));
			inv.addModel("qCount", qCount);
			inv.addModel("qList", qList);	
			inv.addModel("qGroupId", group);
			inv.addModel("curpage", curpage);
			inv.addModel("itemPerPage", itemPerPage);
			inv.addModel("nav", "group");
			inv.addModel("pagerUrl", inv.getModel("HomeUrl")+"/QList/grouplist/"+group + "/");
			return "qList";
		}
}